package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.service.MarketTopicService;
import com.cskaoyan.market.service.impl.MarketTopicServiceImpl;
import com.cskaoyan.market.util.GetRequestBodyUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;


import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/topic/*")
public class AdminTopicController extends HttpServlet {
    private MarketTopicService marketTopicService = new MarketTopicServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = getOpString(req);
        //查询专题接口
        if ("list".equals(op)) {
            list(req, resp);
        }
//读取要修改的专题信息，返回给编辑页面
        if ("read".equals(op)) {
            read(req, resp);
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opString = getOpString(req);
        //修改专题接口
        if ("update".equals(opString)) {
            update(req, resp);
        }
        //删除专题接口
        if ("delete".equals(opString)) {
            delete(req, resp);
        }
        //添加专题接口
        if ("create".equals(opString)) {
            create(req, resp);
        }
    }


    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        //获取专题Id，根据Id逻辑删除专题
        String id = JacksonUtil.parseString(requestBody, "id");
        marketTopicService.deleteTopic(id);
        String responseBody = JacksonUtil.writeValueAsString(ResponseUtil.ok());
        resp.getWriter().println(responseBody);
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        //将Json格式的字符串转成对应添加专题的专题实体类
        MarketTopic marketTopic = JacksonUtil.getObjectMapper().readValue(requestBody, MarketTopic.class);
        //返回一个装对应返回信息的专题实体类
        MarketTopic resultTopic = marketTopicService.createTopic(marketTopic);
        String respBody = JacksonUtil.writeValueAsString(ResponseUtil.ok(resultTopic));
        resp.getWriter().println(respBody);
    }


    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        //将Json字符串解析成对应修改条件的专题实体类
        MarketTopic record = JacksonUtil.getObjectMapper().readValue(requestBody, MarketTopic.class);
        //根据专题实体类执行修改
        MarketTopic updatedTopic = marketTopicService.updateTopic(record);
        String asString = JacksonUtil.writeValueAsString(ResponseUtil.ok(updatedTopic));
        resp.getWriter().println(asString);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        //title和subtitle不是必须的请求参数，可能获取为null
        String title = req.getParameter("title");
        if (title == null) {
            title = "";
        }
        String subtitle = req.getParameter("subtitle");
        if (subtitle == null) {
            subtitle = "";
        }
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
//        根据获取参数执行查询
        List<MarketTopic> topics = marketTopicService.getTopics(page, limit, title, subtitle, sort, order);
        String jsonResult = JacksonUtil.writeValueAsString(ResponseUtil.okList(topics));
        resp.getWriter().println(jsonResult);
    }

    private void read(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
//        根据Id获取从Service层封装好的对应响应体Json格式的Map容器
        Map<String, Object> result = marketTopicService.getTopicById(id);
        String jsonResult = JacksonUtil.writeValueAsString(ResponseUtil.ok(result));
        resp.getWriter().println(jsonResult);
    }

    //分发请求
    private String getOpString(HttpServletRequest req) {
        String requestURI = req.getRequestURI();
        return requestURI.replace(req.getContextPath() + "/admin/topic/", "");
    }
}
